package org.lwd.datalink.domain.task;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.type.Alias;
import org.lwd.datalink.domain.BaseEntity;
import org.lwd.datalink.domain.Storable;
import org.lwd.datalink.domain.plugin.PluginReaderParameter;
import org.lwd.datalink.domain.plugin.PluginWriterParameter;
import org.lwd.datalink.errors.DatalinkException;

import java.io.Serializable;
import java.util.List;

/**
 * Task配置类
 */
@Alias("task")
@Getter
@Setter
public class TaskInfo extends BaseEntity implements Serializable, Storable, Comparable<TaskInfo>, Cloneable {


    //------------------------------------------------------------------------------------------------------------------
    // ----------------------------------------------fields mapping to database-----------------------------------------
    //------------------------------------------------------------------------------------------------------------------

    private Long id;
    private String taskName;
    private String taskDesc;
    private String taskParameter;
    private Long readerMediaSourceId;
    private String taskReaderParameter;
    private String taskWriterParameter;
    private TargetState targetState;
    private TaskType taskType;
    private Long groupId;
    private Long leaderTaskId;
    private boolean leaderTaskFlag;
    private String taskSyncMode;
    private Long alarmPriorityId;
    private Integer taskPriorityId;

    public void setTaskParameter(String taskParameter) {
        this.taskParameter = taskParameter;
        if (!StringUtils.isEmpty(taskParameter)) {
            taskParameterObj = JSONObject.parseObject(taskParameter, new TypeReference<TaskParameter>() {}, Feature.SupportAutoType);
            taskParameterObj.setTaskId(id);
        } else {
            taskParameterObj = null;
        }
    }

    public void setTaskReaderParameter(String taskReaderParameter) {
        this.taskReaderParameter = taskReaderParameter;
        if (!StringUtils.isEmpty(taskReaderParameter)) {
            taskReaderParameterObj = JSONObject.parseObject(taskReaderParameter, new TypeReference<PluginReaderParameter>() {}, Feature.SupportAutoType);
        } else {
            taskReaderParameterObj = null;
        }
    }

    public void setTaskWriterParameter(String taskWriterParameter) {
        this.taskWriterParameter = taskWriterParameter;
        if (!StringUtils.isEmpty(taskWriterParameter)) {
            taskWriterParameterObjs = JSONObject.parseArray(taskWriterParameter, PluginWriterParameter.class);
        } else {
            taskWriterParameterObjs = null;
        }
    }

    public TargetState getTargetState() {
        return targetState;
    }


//------------------------------------------------------------------------------------------------------------------
    //-----------------------------------------fields for business------------------------------------------------------
    //------------------------------------------------------------------------------------------------------------------

    private transient Long version;//version代表的是整个分组配置的版本
    private transient PluginReaderParameter taskReaderParameterObj;
    private transient List<PluginWriterParameter> taskWriterParameterObjs;
    private transient TaskParameter taskParameterObj;

    public String idString() {
        return String.valueOf(id);
    }
    //------------------------------------------------------------------------------------------------------------------
    //-----------------------------------------business methods---------------------------------------------------------
    //------------------------------------------------------------------------------------------------------------------

    public PluginReaderParameter getTaskReaderParameterObj() {
        if (taskReaderParameterObj == null) {
            throw new DatalinkException("Task-reader-parameter can not be null.");
        }
        return taskReaderParameterObj;
    }

    public List<PluginWriterParameter> getTaskWriterParameterObjs() {
        if (taskWriterParameterObjs == null) {
            throw new DatalinkException("Task-writer-parameter can not be null.");
        }
        return taskWriterParameterObjs;
    }

    public TaskParameter getTaskParameterObj() {
        if (taskParameterObj == null) {
            throw new DatalinkException("Task-parameter can not be null.");
        }
        return taskParameterObj;
    }

    public Long getModifyTimeMillSeconds() {
        return getUpdateTime().getTime();
    }

    @Override
    public int compareTo(TaskInfo o) {
        if (this.getId().longValue() > o.getId().longValue()) {
            return 1;
        } else if (this.getId().longValue() == o.getId().longValue()) {
            return 0;
        } else {
            return -1;
        }
    }

    @Override
    public String toString() {
        return "TaskInfo{" +
                "id=" + id +
                ", taskName='" + taskName + '\'' +
                ", taskDesc='" + taskDesc + '\'' +
                ", readerMediaSourceId=" + readerMediaSourceId +
                ", taskType=" + taskType +
                ", groupId=" + groupId +
                ", leaderTaskId=" + leaderTaskId +
                ", isLeaderTask=" + leaderTaskFlag +
                ", taskSyncMode='" + taskSyncMode + '\'' +
                ", version=" + version +
                '}';
    }

    @Override
    public TaskInfo clone() throws CloneNotSupportedException {
        return (TaskInfo) super.clone();
    }
}
